<?php
/**
 * 
 *
 * @link http://ad.zjol.com.cn/
 * @copyright 2005  WAM Chian Ltd.
 * @author gabriel <deming@staff.service4media.com>
 * @version 1.0
 */

class Validation
{
	private $execinfo = '';

	public function isChar($content)
	{
		if(ereg('^[a-zA-Z]+$',$content))
			return TRUE;
		else
			return FALSE;
	}
	
	public function isCharVSnumber($content)
	{
		if(ereg('^[a-zA-Z0-9]+$',$content))
			return TRUE;
		else
			return FALSE;
	}

	public function isNumber($content)
	{
		if(ereg("^[0-9]{1,}$",$content))
			return TRUE;
		else
			return FALSE;
	}
	public function isEmail($content,$allowEmpty = 'notEmpty')
	{	
		if($allowEmpty == 'empty' AND empty($content))
			return TRUE;
		if (eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$",$content))
			return TRUE;
		else
			return FALSE;
	}
	public function isUrl($content,$allowEmpty = 'notEmpty')
	{
		if($allowEmpty == 'empty' AND empty($content))
			return TRUE;
		if (preg_match('/^http:\/\/[a-z0-9@:%_.~#-\?&]+$/i',$content))
			return TRUE;
		else
			return FALSE;
	}
	public function isDate($content)
	{
		if (preg_match('/[0-2]\d\d\d-[01]\d-[0-3]\d/i',$content))
			return TRUE;
		else
			return FALSE;
	}
	public function isChinese($content,$allowEmpty = 'notEmpty')
	{
		if($allowEmpty == 'empty' AND empty($content))
			return TRUE;
		if (eregi("^[\xa0-\xff]{0,}$",$content))
			return TRUE;
		else
			return FALSE;
	}
	
	public function isPaper18($content)
	{
		if(strlen($content)==18){
			for ($i=0;$i<18;$i++){
				$arrPaper[$i]=substr($content,$i,1);
			}
			$arrGene=array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
			$intTmp=0;
			for ($i=0;$i<17;$i++){
				$intTmp+=$arrPaper[$i]*$arrGene[$i];
			}
			$arrCheckCode=array('1','0','X','9','8','7','6','5','4','3','2');
			$intCheckPosition=$intTmp%11;
			if($arrCheckCode[$intCheckPosition]==strtoupper($arrPaper[17]))
				return TRUE;
			else{
				$this->execinfo='������ĺ��벻�����֤����!';
				return FALSE;
			}
		}else{
			$this->execinfo='������ĺ��벻��18λ!';
			return FALSE;
		}
	}
	public function goto15To18Paper($content)
	{
		if(strlen($content)==15 AND $this->isNumber($content)){
			$arrGene=array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
			$arrCheckCode=array('1','0','X','9','8','7','6','5','4','3','2');
			$strTmp=substr($content,0,6);
			$strTmpContent=$strTmp.'19'.substr($content,6);
			for ($i=0;$i<18;$i++){
				$arrPaper[$i]=substr($strTmpContent,$i,1);
			}
			$intTmp=0;
			for ($i=0;$i<17;$i++){
				$intTmp+=$arrPaper[$i]*$arrGene[$i];
			}
			$intCheckPosition=$intTmp%11;
			return $strTmpContent.$arrCheckCode[$intCheckPosition];
		}else{
			$this->execinfo='������ĺ��벻��15λ��������!';
			return FALSE;
		}
	}
	public function isPaper15($content)
	{
		if(preg_match("/^\d{15}$/i",$content)){
			$arrAera=array(11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91);
			if(in_array(substr($content,0,2),$arrAera)){
				if(checkdate(substr($content,8,2),substr($content,10,2),'19'.substr($content,6,2)))
					return TRUE;
				else{
					$this->execinfo='����������֤�����ǷǷ�������!';
					return FALSE;
				}
			}else{
				$this->execinfo='����������֤�����ǷǷ�����!';
				return FALSE;
			}
		}else{
			$this->execinfo='������ĺ��벻��15λ��������!';
			return FALSE;
		}
	}
	public function isPaper($content)
	{
		if(strlen($content) != 15 AND strlen($content) != 18){
			$this->execinfo='������ĺ��벻��15λ��18λ�ĺ���!';
			return FALSE;
		}else if(strlen($content) == 15){
			if($this->isPaper15($content))
				return TRUE;
			else
				return FALSE;
		}else{
			if($this->isPaper18($content))
				return TRUE;
			else
				return FALSE;
		}
	}
}
?>
